package com.aim.analysis.records;

import com.aim.util.filters.Filter;
import com.aim.util.filters.FilterUtil;
import com.aim.util.HashMapOfList;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: User
 * Date: 6/4/14
 * Time: 10:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class GameMap {

    private Map<GameKey, Game> gameMap = new HashMap<GameKey, Game>();
    private HashMapOfList<String, Game> playerMap = new HashMapOfList<String, Game>();

    private Filter<GameRecord> recordFilter;

    public GameMap() {
        this(FilterUtil.dummy(GameRecord.class));
    }

    public GameMap(Filter<GameRecord> recordFilter) {
        this.recordFilter = recordFilter;
    }

    public void addRecord(GameRecord record) {
        if (recordFilter.accept(record)) {
            Game game = getOrCreateGame(record.gameKey);
            game.addRecord(record);
        }
    }

    private Game getOrCreateGame(GameKey gameKey) {
        Game game = gameMap.get(gameKey);
        if (game == null){
            game = new Game(gameKey);
            gameMap.put(gameKey, game);
            playerMap.put(gameKey.partitionKey, game);
        }
        return game;
    }

    public void postProcessing(){
        for (Game game : getAllGames()) {
            game.postProcessing();
        }
    }

    public List<Game> getAllGames(){
        return Collections.unmodifiableList(new ArrayList(gameMap.values()));
    }

    public List<Game> getByName(String assignmentId){
        return playerMap.get(assignmentId);
    }
}
